/**
 * Copyright (C), 2018 store
 * Encoding: UTF-8
 * Date: 19-4-23 下午6:05
 * History:
 */
package com.swak.frame.api.filter.spi;

import org.apache.commons.lang3.StringUtils;
import org.jboss.logging.MDC;
import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.swak.frame.chain.FilterInvoker;
import com.swak.frame.context.BoundContext;
import com.swak.frame.filter.ApiFilter;
import com.swak.frame.util.UUIDHexGenerator;

/**
 * ChannelContextFilter.java 上下文
 * 
 * @author ColleyMa
 * @version 19-4-23 下午6:05
 */
public class LogMdcFilter implements ApiFilter {
	
	private Logger logger = LoggerFactory.getLogger(this.getClass());
	
	private static final String TRACE_ID = "txId";
	
	private static final String PIN ="PIN";
	
	@Override
	public void doFilter(BoundContext context, FilterInvoker<BoundContext> nextFilter) {
		
		logger.info("start doFilter:{}", DateTime.now().toString());
		
		MDC.put(TRACE_ID, UUIDHexGenerator.generator().hashCode() & Integer.MAX_VALUE);
		String pin = context.getRequest().getPin();
		if(StringUtils.isNoneEmpty(pin)) {
			MDC.put(PIN, pin);
		}
		//下一个流程
		nextFilter.invoke(context);
	}
}
